<?php

if (!function_exists("file_get_contents"))
{
	function file_get_contents($path)
	{
		if (!file_exists($path)) return false;
		$fp=@fopen($path,"r");
		$all=fread($fp,filesize($path));
		fclose($fp);
		return $all;
	}
}

if (!function_exists("file_put_contents"))
{
	function file_put_contents($path,$val)
	{
		$fp=@fopen($path,"w");
		fputs($fp,$val);
		fclose($fp);
		return true;
	}
}

function my_encode($val)
{
	return md5(strrev(base64_encode($val)));
}

function exitjs($alert,$url)
{
	die("<script language=javascript>alert(\"".AddSlashes($alert)."\");window.location=\"".AddSlashes($url)."\";</script>");
}

function inlog($str)
{
	return true;
}

function dealsize($s)
{
	$danwei = array( "Byte","KB","MB","GB" );
	$d = 0;
	while ( $s >= 900 )
	{
		$s = round($s*100/1024)/100;
		$d++;
	}
	return $s." ".$danwei[$d];
}

function basename1($s)
{
	$arr=explode("/",$s);
	return $arr[count($arr)-1];
}

function deal_temp($path,$value=false)
{
	if (!file_exists($path)) return false;
	$all = @file_get_contents($path);
	if (!is_array($value)) return $all;
	$arr=array_keys($value);
	$keys=array();
	foreach($arr as $k) $keys[]='{'.$k.'}';
	return str_replace($keys,$value,$all);
}

function dealpath($path)
{
	global $root,$user;
	if (!$root) $root = $user["root"];
	if (!$root) return false;
	$path = trim($path);
	if ($root == $path) return $path;
	$arr = explode($root,$path);
	if ($arr[0]) return false;
	$path = str_replace($root,"",$path);
	$path = str_replace("../","",$path);
	return $root.$path;
}

function get_micro()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

function urldecode1($s)
{
	$s=str_replace("+","%2B",$s);
	return urldecode($s);
}

function urlencode1($s)
{
	$url = urlencode($s);
	return str_replace(array('%2F','+'),array('/','%20'),$url);
}

function decode_path($path)
{
	$arr = explode('/',$path);
	$re = '';

	foreach($arr as $l)
	{
		if (!trim($l)) continue;
		if ($l == '.')
		{
			$re && $re.= '/';
			$re.= '.';
		}
		else
		{
			$re && $re.= '/';
			$re.= name_decode($l);
		}
	}
	return $re;
}

function array2json($arr)
{
	$keys = array_keys($arr);
	$isarr = true;
	$json = "";
	for($i=0;$i<count($keys);$i++)
	{
		if ($keys[$i] !== $i)
		{
			$isarr = false;
			break;
		}
	}
	$json = $space;
	$json.= ($isarr)?"[":"{";
	for($i=0;$i<count($keys);$i++)
	{
		if ($i!=0) $json.= ",";
		$item = $arr[$keys[$i]];
		$json.=($isarr)?"":$keys[$i].':';
		if (is_array($item))
			$json.=array2json($item);
		else if (is_string($item))
			$json.='"'.str_replace(array("\r","\n"),array('\r','\n'),$item).'"';
		else $json.=$item;
	}
	$json.= ($isarr)?"]":"}";
	return $json;
}

function get_encode($path)
{
	global $config;
	$utf_8 = chr(239).chr(187);
	$unicode = chr(255).chr(254);
	$fp = @fopen($path,"rb");
	$a = @fread($fp,2);
	$word = $a.fread($fp,filesize($path));
	@fclose($fp);
	if ($a == $utf_8) return "UTF-8";
	else if ($a == $unicode) return "Unicode";
	else
	{
		if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word) == true)
		{
			return "UTF-8";
		}
		else
		{
			return $config['default_charset_of_textfile'];
		}
	}
}


function get_folder_size($path)
{
	if (!$path || !is_dir($path)) return false;
	@exec("du -sh ".$_SESSION['repo_path'],$re_repo);
	@exec("du -sh --exclude .svn ".$path,$re_doc_exclude_svn);
	
	$re = size_to_byte($re_repo[0]);
	if ($re_doc_exclude_svn)
	{
		//return size_to_byte($re[0]+424660000);
		return $re+size_to_byte($re_doc_exclude_svn[0]);
	}
	else return $re;
}

function size_to_byte($s)
{
	$size = intval($s);
	if (strpos($s,'K') !== false)
	{
		$size = $size * 1024;
	}
	else if (strpos($s,'M') !== false)
	{
		$size = $size * 1024 * 1024;
	}
	else if (strpos($s,'G') !== false)
	{
		$size = $size*1024*1024*1024;
	}
	return $size;
}

function check_user_space()
{
	global $config;
	$root = $_SESSION['user']['root'];
	$customer_used_size = get_folder_size($root);
	$customer_total = size_to_byte($config['default_customer_size']);
	return ($customer_used_size < $customer_total);
}

